"""
公共接口
"""

import os
from db import models
from conf import settings


# 登陆接口
def login_interface(username, password, user_type):
    # 调用类实例化得到对象
    if user_type == 'admin':
        obj = models.Admin.select(username)

    elif user_type == 'teacher':
        obj = models.Teacher.select(username)

    elif user_type == 'student':
        obj = models.Student.select(username)

    else:
        return False ,'视图对象不在规定范围之内~~~'

    if obj:
        if password == obj.password:
            return True, f'当前用户：{username} 登陆成功！'
        else:
            return False, '密码不一致，请重新输入！'

    else:
        # 若不存在，则证明用户不存在并返回给视图层
        return False, '用户不存在！'
# 获得所有的学校接口
def get_all_school_interface():

    school_path = os.path.join(
        settings.DB_DIR_path, 'School'
    )

    if not os.path.exists(school_path):
        return False, '无学校，请联系管理员处理~~~'

    school_list = os.listdir(school_path)

    return True, school_list